package com.zhijian.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.PersonDiathesisAbilityEstimateMapper;
import com.zhijian.medical.entity.po.PersonDiathesisAbilityEstimate;
import com.zhijian.medical.entity.vo.request.PersonDiathesisAbilityEstimateSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonDiathesisAbilityEstimateSearchVo;
import com.zhijian.medical.entity.vo.response.PersonDiathesisAbilityEstimateListVo;
import com.zhijian.medical.entity.vo.response.PersonDiathesisAbilityEstimateVo;
import com.zhijian.medical.service.PersonDiathesisAbilityEstimateService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 人员-素质能力评估业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-27 13:52:37
 */
@Slf4j
@Service
public class PersonDiathesisAbilityEstimateServiceImpl implements PersonDiathesisAbilityEstimateService {

    @Autowired
    private PersonDiathesisAbilityEstimateMapper mapper;

    @Override
    public PersonDiathesisAbilityEstimate selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PersonDiathesisAbilityEstimateVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PersonDiathesisAbilityEstimate personDiathesisAbilityEstimate) {
        Date now = new Date();
        personDiathesisAbilityEstimate.setCreateTime(now);
        personDiathesisAbilityEstimate.setUpdateTime(now);
        return mapper.insert(personDiathesisAbilityEstimate) > 0 ? personDiathesisAbilityEstimate.getId() : 0L;
    }

    @Override
    public Long insert(PersonDiathesisAbilityEstimateSaveOrUpdateVo personDiathesisAbilityEstimateSaveOrUpdateVo) {
        PersonDiathesisAbilityEstimate insertParam = new PersonDiathesisAbilityEstimate();
        BeanUtils.copyProperties(personDiathesisAbilityEstimateSaveOrUpdateVo, insertParam);
        if (StringUtils.isNotBlank(personDiathesisAbilityEstimateSaveOrUpdateVo.getExamineDate())) {
            insertParam.setExamineDate(DateUtil.parseDate(personDiathesisAbilityEstimateSaveOrUpdateVo.getExamineDate()));
        }
        return this.insert(insertParam);
    }

    @Override
    public boolean update(PersonDiathesisAbilityEstimate personDiathesisAbilityEstimate) {
        Date now = new Date();
        personDiathesisAbilityEstimate.setUpdateTime(now);
        return mapper.updateByPrimaryKey(personDiathesisAbilityEstimate) != 0;
    }

    @Override
    public boolean update(PersonDiathesisAbilityEstimateSaveOrUpdateVo personDiathesisAbilityEstimateSaveOrUpdateVo) {
        PersonDiathesisAbilityEstimate updateParam = new PersonDiathesisAbilityEstimate();
        BeanUtils.copyProperties(personDiathesisAbilityEstimateSaveOrUpdateVo, updateParam);
        if (StringUtils.isNotBlank(personDiathesisAbilityEstimateSaveOrUpdateVo.getExamineDate())) {
            updateParam.setExamineDate(DateUtil.parseDate(personDiathesisAbilityEstimateSaveOrUpdateVo.getExamineDate()));
        }
        return this.update(updateParam);
    }

    @Override
    public Integer count(PersonDiathesisAbilityEstimateSearchVo personDiathesisAbilityEstimateSearchVo) {
        return mapper.count(personDiathesisAbilityEstimateSearchVo);
    }

    @Override
    public List<PersonDiathesisAbilityEstimateListVo> selectList(PersonDiathesisAbilityEstimateSearchVo personDiathesisAbilityEstimateSearchVo) {
        return mapper.selectList(personDiathesisAbilityEstimateSearchVo);
    }

    @Override
    public Page<PersonDiathesisAbilityEstimateListVo> selectPage(PersonDiathesisAbilityEstimateSearchVo personDiathesisAbilityEstimateSearchVo) {
        //先直接查总数
        int count = count(personDiathesisAbilityEstimateSearchVo);
        //分页
        Page<PersonDiathesisAbilityEstimateListVo> page = new Page<>(true, count, personDiathesisAbilityEstimateSearchVo.getPageNum(), personDiathesisAbilityEstimateSearchVo.getPageSize());
        personDiathesisAbilityEstimateSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PersonDiathesisAbilityEstimateListVo> responseVos = this.selectList(personDiathesisAbilityEstimateSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public PersonDiathesisAbilityEstimateVo selectVoByPersonId(Long personId) {
        return mapper.selectVoByPersonId(personId);
    }

}